package com.controller.backend;

import com.alibaba.fastjson.JSON;
import com.biz.*;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.model.*;
import com.github.pagehelper.PageInfo;
import com.sms.SmsUtil;
import com.util.SystemConstant;
import com.util.webpage.PageData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by jc on 2019/12/18.
 */
@Controller
@RequestMapping("/backend/security/billRefund")
public class BillRefundController extends BaseController<BillRefund> {
    @Autowired
    private BillRefundBiz billRefundBiz;

    @Autowired
    private BillBiz billBiz;

    @Autowired
    private SysOperatingLogBiz sysOperatingLogBiz;


    @RequestMapping(value = "cancelIndex")
    public String cancelIndex() throws Exception {
        return "/backend/security/billRefund/cancelIndex";
    }

    @RequestMapping(value = "completeIndex")
    public String completeIndex() throws Exception {
        return "/backend/security/billRefund/completeIndex";
    }

    @RequestMapping(value = "breachIndex")
    public String breachIndex() throws Exception {
        return "/backend/security/billRefund/breachIndex";
    }

    //退款订单详情
    @RequestMapping(value = "detail")
    public String detail() throws Exception {
        return "/backend/security/billRefund/form";
    }


    //获取退款订单列表详情
    @RequestMapping(value = "billRefundCancelList")
    @ResponseBody
    public Result billRefundCancelList(HttpServletRequest request) {
        PageData cond = new PageData(request);
        List<BillRefund> list = billRefundBiz.billRefundCancelList(cond);
        PageInfo<BillRefund> pageInfo = new PageInfo<BillRefund>(list);
        Integer total = (int) pageInfo.getTotal();
        return ResultGenerator.genSuccessResult(list, total);
    }


    @RequestMapping(value = "billRefundCompleteList")
    @ResponseBody
    public Result billRefundCompleteList(HttpServletRequest request) {
        PageData cond = new PageData(request);
        List<BillRefund> list = billRefundBiz.billRefundCompleteList(cond);
        PageInfo<BillRefund> pageInfo = new PageInfo<BillRefund>(list);
        Integer total = (int) pageInfo.getTotal();
        return ResultGenerator.genSuccessResult(list, total);
    }

    //退款成功操作
    @RequestMapping(value = "updateBillRefund")
    @ResponseBody
    public Result updateBillRefund(BillRefund billRefund, HttpSession session) {
        ManagerInfo managerInfo = (ManagerInfo) session.getAttribute(SystemConstant.SESSION_ADMIN);

        if (billRefund != null) {
            baseBiz.update(billRefund);
        }
        BillRefund newRefund = billRefundBiz.findById(billRefund.getId());

        Bill bill = billBiz.findById(newRefund.getBillId());

        SysOperatingLog sysOperatingLog = new SysOperatingLog();
        sysOperatingLog.setBeforeLog(JSON.toJSONString(bill));
        sysOperatingLog.setOperatingId(managerInfo.getId());
        sysOperatingLog.setOperatingName(managerInfo.getUserName());

        bill.setStatus(5);
        billBiz.update(bill);

        sysOperatingLog.setAfterLog(JSON.toJSONString(bill));
        sysOperatingLog.setRemark("退款成功操作");
        sysOperatingLogBiz.save(sysOperatingLog);

        return ResultGenerator.genSuccessResult();
    }
}
